﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ShopOnline.Controls;
using POSSystem.BLL;
using ShopOnline;

public partial class Controls_Menu : System.Web.UI.UserControl
{
    protected void btnSearch_OnClick(object sender, EventArgs e)
    {
        if (txtSearch.Text != "" && txtSearch.Text != "Type a keyword")
            Response.Redirect(this.ResolveUrl("~/Search.aspx?key=") + txtSearch.Text);
    }

    const string KEY = "key";

    protected void Page_Load(object sender, EventArgs e)
    {
        ScriptManager.RegisterStartupScript(this, this.GetType(), ClientID, "initMenu();", true);
        if (!IsPostBack)
        {
            if (!string.IsNullOrEmpty(Request.QueryString[KEY]))
            {
                txtSearch.Text = Request.QueryString[KEY];
            }

            menu.Items = new System.Collections.Generic.List<MyMenuItem>();
            menu.Items.Add(new MyMenuItem("Business Card", "businesscard", this.ResolveClientUrl("~/BusinessCard.aspx")));

            Categories cat = null;
            if (View_Customers.GetCurrentUser() == null || (View_Customers.GetCurrentUser().RowCount > 0 && View_Customers.GetCurrentUser().Price == CustomerType.CustomerPrice.SalePrice))
                cat = Categories.GetAllWebActiveCategoriesByParent(null, false);
            else
                cat = Categories.GetAllWebActiveCategoriesByParent(null);

            cat.Filter = Categories.ColumnNames.ParentID + " IS NULL";
            for (int i = 0; i < Math.Min(cat.RowCount, 7); i++)
            {
                MyMenuItem item = new MyMenuItem(cat.s_CategoryName, cat.s_ID, this.ResolveClientUrl(string.Format("~/Search.aspx?{0}=", BasePage.CAT) + string.Format("{0}_{1}", ShopOnline.Utilities.GetValidParam(cat.s_CategoryName), cat.s_ID)));
                if (i == Math.Min(cat.RowCount, 7) - 1)
                    item.Last = true;
                menu.Items.Add(item);

                Categories catChild = null;
                if (View_Customers.GetCurrentUser() == null || (View_Customers.GetCurrentUser().RowCount > 0 && View_Customers.GetCurrentUser().Price == CustomerType.CustomerPrice.SalePrice))
                    catChild = Categories.GetAllWebActiveCategoriesByParent(cat.ID, false);
                else
                    catChild = Categories.GetAllWebActiveCategoriesByParent(cat.ID);

                for (int j = 0; j < catChild.RowCount; j++)
                {
                    item.Items.Add(new MyMenuItem(catChild.s_CategoryName, catChild.s_ID, this.ResolveClientUrl(string.Format("~/Search.aspx?{0}=", BasePage.CAT) + string.Format("{0}_{1}", ShopOnline.Utilities.GetValidParam(catChild.s_CategoryName), catChild.s_ID))));
                    catChild.MoveNext();
                }
                cat.MoveNext();
            }
        }
    }
}